

#################### READ IN Study Number 5840 - Scottish Social Attitudes Survey, 2006 ############
library(foreign)
ssas.2006 <- read.dta("Article.Dirichlet/Data.ESDS/UKDA-5840-stata8/stata8/ssa2006.dta")

ssas.df <- data.frame(
	"househld"  = ssas.2006$househld,		# number of people living in household
	"rsex"      = ssas.2006$rsex,			# respondent's sex
	"rage"      = ssas.2006$rage,			# respondent's age
	"smsexpar"  = ssas.2006$smsexpar,		# opposite, same, none, >1
	"working"   = ssas.2006$reconact,		# working status
	"sreadpap"  = ssas.2006$sreadpap,		# reads paper regularly
	"relgsums"  = ssas.2006$relgsums,		# religion summary variable
	"ptyallgs"  = ssas.2006$ptyallgs,		# party allegience
	"soctrust"  = ssas.2006$soctrust,		# social trust
	"idlosem"   = ssas.2006$idlosem,		# more Muslims erodes Scottish identity, pos=racism
	"marrmus"   = ssas.2006$marrmus,		# okay to marry Muslims, positive=racism 
	"slivie12"  = ssas.2006$slivie12,		# standard of living improving in Scotland?
	"ukintnat"  = ssas.2006$ukintnat,		# UK gov works in Scotlands long-term interests
	"natinnat"  = ssas.2006$natinnat,		# Scottish exec works in Scotlands long-term interests"
	"voiceuk3"  = ssas.2006$voiceuk3,		# Scot Par gives Scotland a greater voice in the UK
	"sayingv3"  = ssas.2006$sayingv3,		# Scot Par gives ordinary people more say in Scotland
	"whrbrn"    = ssas.2006$whrbrn,		        # born in Scotland?
	"scotpar2"  = ssas.2006$scotpar2,		# Scotland independence, see page 77
	"nhssat"    = ssas.2006$nhssat,			# satisfaction with national Health Service
	"hincdif2"  = ssas.2006$hincdif2,		# living comfortably on current income
	"unionsa"   = ssas.2006$unionsa,		# in union or staff association
	"hedqual2"  = ssas.2006$hedqual2		# education level
) 

########################## RECODE VARIABLES ########################################################
ssas.df$rsex      <- 2 - as.numeric(ssas.df$rsex)       	# 0=female, 1=male
ssas.df$rage      <- as.numeric(ssas.df$rage);          ssas.df$rage[ssas.df$rage==-1] <- NA # REFUSAL
ssas.df$smsexpar  <- - as.numeric(ssas.df$smsexpar)     	# 1=opposite sex, 2=same sex, 3=no partner, 4=>1 partner 
ssas.df$working   <- as.numeric(ssas.df$working);       ssas.df$working[ssas.df$working<=4] <-  1    # working or student
                                                        ssas.df$working[ssas.df$working >7] <-  0    # retired, disab or homemaker
                                                        ssas.df$working[ssas.df$working >4] <- -1    # unemployed
ssas.df$sreadpap  <- 2 - as.numeric(ssas.df$sreadpap)
ssas.df$relgsums  <- 7 - as.numeric(ssas.2006$relgsums) 	# 1=C.of S, 0=other
                                                        ssas.df$relgsums[ssas.df$relgsums==-1] <- NA # REFUSAL
                                                        ssas.df$relgsums[ssas.df$relgsums < 4] <- 0 # REFUSAL
                                                        ssas.df$relgsums[ssas.df$relgsums == 4] <- 1 # REFUSAL
ssas.df$ptyallgs  <- as.numeric(ssas.df$ptyallgs);      ssas.df$ptyallgs[ssas.df$ptyallgs==15] <- NA # see page 19
ssas.df$soctrust  <- as.numeric(ssas.df$soctrust);      ssas.df$soctrust[ssas.df$soctrust==2] <-  1 # trusts people
                                                        ssas.df$soctrust[ssas.df$soctrust==3] <- -1 # does not trust people
                                                        ssas.df$soctrust[ssas.df$soctrust==4] <-  0 # does not know
                                                        ssas.df$soctrust[ssas.df$soctrust==5] <- NA # REFUSAL
ssas.df$idlosem  <- as.numeric(ssas.df$idlosem);        ssas.df$idlosem[ssas.df$idlosem==2] <- 1
                                                        ssas.df$idlosem[ssas.df$idlosem==8] <- NA
                                                        ssas.df$idlosem[ssas.df$idlosem > 1] <- 0
ssas.df$marrmus  <- as.numeric(ssas.df$marrmus);        ssas.df$marrmus[ssas.df$marrmus==6] <- 1     # move unhappy to 1
                                                        ssas.df$marrmus[ssas.df$marrmus==7] <- 1     # move very unhappy to 1
                                                        ssas.df$marrmus[ssas.df$marrmus !=1] <- 0   
ssas.df$slivie12 <- 4 - as.numeric(ssas.2006$slivie12); ssas.df$slivie12[ssas.df$slivie12==-3] <- 0  # move DK to center
ssas.df$ukintnat <- 5 - as.numeric(ssas.df$ukintnat);   ssas.df$ukintnat[ssas.df$ukintnat==-1] <- 0
ssas.df$natinnat <- 5 - as.numeric(ssas.df$natinnat);   ssas.df$natinnat[ssas.df$natinnat==-1] <- 0
ssas.df$voiceuk3 <- 4 - as.numeric(ssas.df$voiceuk3);   ssas.df$voiceuk3[ssas.df$voiceuk3==-1] <- 0
ssas.df$sayingv3 <- 4 - as.numeric(ssas.df$sayingv3);   ssas.df$sayingv3[ssas.df$sayingv3==-1] <- 0
ssas.df$whrbrn   <- 3 - as.numeric(ssas.df$whrbrn);     ssas.df$whrbrn[ssas.df$whrbrn !=2] <- 0      # zero if not born in S
                                                        ssas.df$whrbrn[ssas.df$whrbrn ==2] <- 1      # one if not born in S
ssas.df$scotpar2 <- as.numeric(ssas.df$scotpar2);       ssas.df$scotpar2[ssas.df$scotpar2 < 4] <- 1    # wants independence
                                                        ssas.df$scotpar2[ssas.df$scotpar2 > 3] <- 0    # no independence
ssas.df$nhssat   <- as.numeric(ssas.df$nhssat);         ssas.df$nhssat[ssas.df$nhssat ==5] <- 1     # quite dissatisfied to 1
                                                        ssas.df$nhssat[ssas.df$nhssat ==6] <- 1     # very dissatisfied to 1
                                                        ssas.df$nhssat[ssas.df$nhssat ==8] <- NA    # REFUSAL
                                                        ssas.df$nhssat[ssas.df$nhssat > 1] <- 0    # REFUSAL
                                                        ssas.df$nhssat[ssas.df$nhssat ==-4] <- NA    # REFUSAL
ssas.df$hincdif2 <- 3 - as.numeric(ssas.df$hincdif2);   ssas.df$hincdif2[ssas.df$hincdif2==-4] <- 0  # DK to 0
                                                        ssas.df$hincdif2[ssas.df$hincdif2< -2] <- NA # OTHER plus REFUSAL
ssas.df$unionsa  <- as.numeric(ssas.df$unionsa);        ssas.df$unionsa[ssas.df$unionsa==-2] <- 1    # combine union and staff
                                                        ssas.df$unionsa[ssas.df$unionsa==5] <- NA    # REFUSAL
                                                        ssas.df$unionsa[ssas.df$unionsa>2] <- 0      # move nonunions to zero
ssas.df$hedqual2 <- 8 - as.numeric(ssas.df$hedqual2);   ssas.df$hedqual2[ssas.df$hedqual2==-1] <- NA # REFUSAL

library(nnet); library(mice)	# REPEAT 10 TIMES
ssas.imp <- mice(ssas.df)
ssas1.df <- complete(ssas.imp,1)
write.table(ssas1.df,file="/Users/jgill/Article.Dirichlet/Paper.Sampling.Schemes/ssas.df",quote=FALSE)
ssas.test <- read.table(file="/Users/jgill/Article.Dirichlet/Paper.Sampling.Schemes/ssas.df",header=TRUE)


